/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pcs;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

/**
 *
 * @author ahmet
 */
public class SystemSettings {
    private static String settingsFileExtension = ".xml";
    private static String settingsFilename = "settings"+settingsFileExtension;

    private Properties settings;

    public SystemSettings() {
        settings = new Properties();
        load();
    }

    private boolean stringToBoolean(String var) {
        if (var.equals("true"))
            return true;

        return false;
    }

    private double stringToDouble(String var) {
        return Double.parseDouble(var);
    }

    private void load() {
        try {
            settings.loadFromXML(new FileInputStream(settingsFilename));
        } catch (FileNotFoundException fnfe) {
            System.out.println("Settings file does not exist. Default file will be created.");
            setDefault();
            save();
            load();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            System.out.println("Settings file can not be read. The file will be overwritten.");
        }

        setWarnDeteriorationWithPopup(stringToBoolean(settings.getProperty("warnDeteriorationWithPopup")));
        setWarnDeteriorationWithSound(stringToBoolean(settings.getProperty("warnDeteriorationWithSound")));
        setWarnDeteriorationWithRestriction(stringToBoolean(settings.getProperty("warnDeteriorationWithRestriction")));
        setWarnDeteriorationTimeUntilRestriction(stringToDouble(settings.getProperty("warnDeteriorationTimeUntilRestriction")));
 
        setWarnForBreakTimeUntilWarning(stringToDouble(settings.getProperty("warnForBreakTimeUntilWarning")));
        setWarnForBreakWithPopup(stringToBoolean(settings.getProperty("warnForBreakWithPopup")));
        setWarnForBreakWithSound(stringToBoolean(settings.getProperty("warnForBreakWithSound")));
        setWarnForBreakWithRestriction(stringToBoolean(settings.getProperty("warnForBreakWithRestriction")));
        setWarnForBreakTimeUntilRestriction(stringToDouble(settings.getProperty("warnForBreakTimeUntilRestriction")));

        setWarnForWorkTimeUntilWarning(stringToDouble(settings.getProperty("warnForWorkTimeUntilWarning")));
        setWarnForWorkWithPopup(stringToBoolean(settings.getProperty("warnForWorkWithPopup")));
        setWarnForWorkWithSound(stringToBoolean(settings.getProperty("warnForWorkWithSound")));
    }

    private boolean warnDeteriorationWithPopup;
    private boolean warnDeteriorationWithSound;
    private boolean warnDeteriorationWithRestriction;
    private double warnDeteriorationTimeUntilRestriction; //In seconds

    private double warnForBreakTimeUntilWarning; //In minutes
    private boolean warnForBreakWithPopup;
    private boolean warnForBreakWithSound;
    private boolean warnForBreakWithRestriction;
    private double warnForBreakTimeUntilRestriction;

    private double warnForWorkTimeUntilWarning; //In minutes
    private boolean warnForWorkWithPopup;
    private boolean warnForWorkWithSound;
    

    private String goodPostureImageFilename;
    private String badPostureImageFilename; 

    private void setDefault() {
        setWarnDeteriorationWithPopup(true);
        setWarnDeteriorationWithSound(false);
        setWarnDeteriorationWithRestriction(false);
        setWarnDeteriorationTimeUntilRestriction(30.0);

        setWarnForBreakTimeUntilWarning(60.0);
        setWarnForBreakWithPopup(true);
        setWarnForBreakWithSound(false);
        setWarnForBreakWithRestriction(false);
        setWarnForBreakTimeUntilRestriction(30.0);

        setWarnForWorkTimeUntilWarning(10.0);
        setWarnForWorkWithPopup(false);
        setWarnForWorkWithSound(false);
    }

    private String booleanToString(boolean var) {
        if (var)
            return "true";

        return "false";
    }

    private String doubleToString(double var) {
        return Double.toString(var);
    }

    public void save() {
        settings.setProperty("warnDeteriorationWithPopup", booleanToString(isWarnDeteriorationWithPopup()));
        settings.setProperty("warnDeteriorationWithSound", booleanToString(isWarnDeteriorationWithSound()));
        settings.setProperty("warnDeteriorationWithRestriction", booleanToString(isWarnDeteriorationWithRestriction()));
        settings.setProperty("warnDeteriorationTimeUntilRestriction", doubleToString(getWarnDeteriorationTimeUntilRestriction()));

        settings.setProperty("warnForBreakTimeUntilWarning", doubleToString(getWarnForBreakTimeUntilWarning()));
        settings.setProperty("warnForBreakWithPopup", booleanToString(isWarnForBreakWithPopup()));
        settings.setProperty("warnForBreakWithSound", booleanToString(isWarnForBreakWithSound()));
        settings.setProperty("warnForBreakWithRestriction", booleanToString(isWarnForBreakWithRestriction()));
        settings.setProperty("warnForBreakTimeUntilRestriction", doubleToString(getWarnForBreakTimeUntilRestriction()));

        settings.setProperty("warnForWorkTimeUntilWarning", doubleToString(getWarnForWorkTimeUntilWarning()));
        settings.setProperty("warnForWorkWithPopup", booleanToString(isWarnForWorkWithPopup()));
        settings.setProperty("warnForWorkWithSound", booleanToString(isWarnForWorkWithSound()));

        try {
            settings.storeToXML(new FileOutputStream(settingsFilename), null);
        } catch (FileNotFoundException fnfe) {
            // ERROR MESSAGE
        } catch (IOException ioe) {
            // ERROR MESSAGE
        }
    }

    private static void copyFile(String source, String destination) {
        try{
            BufferedReader br = new BufferedReader(new FileReader(source));
            PrintWriter pw = new PrintWriter(destination);
            String line="";
            while ((line=br.readLine())!=null) {
                pw.println(line);
                pw.flush();
            }
            br.close();
            pw.close();
        } catch(IOException ioe){
            // ERROR MESSAGE
        }
    }

    public void importFrom(String importFilename) {
        copyFile(importFilename, settingsFilename);
        load();
    }

    public void exportTo(String exportFilename) {
        copyFile(settingsFilename, exportFilename);
    }

    /**
     * @return the warnDeteriorationWithPopup
     */
    public boolean isWarnDeteriorationWithPopup() {
        return warnDeteriorationWithPopup;
    }

    /**
     * @param warnDeteriorationWithPopup the warnDeteriorationWithPopup to set
     */
    public void setWarnDeteriorationWithPopup(boolean warnDeteriorationWithPopup) {
        this.warnDeteriorationWithPopup = warnDeteriorationWithPopup;
    }

    /**
     * @return the warnDeteriorationWithSound
     */
    public boolean isWarnDeteriorationWithSound() {
        return warnDeteriorationWithSound;
    }

    /**
     * @param warnDeteriorationWithSound the warnDeteriorationWithSound to set
     */
    public void setWarnDeteriorationWithSound(boolean warnDeteriorationWithSound) {
        this.warnDeteriorationWithSound = warnDeteriorationWithSound;
    }

    /**
     * @return the warnDeteriorationWithRestriction
     */
    public boolean isWarnDeteriorationWithRestriction() {
        return warnDeteriorationWithRestriction;
    }

    /**
     * @param warnDeteriorationWithRestriction the warnDeteriorationWithRestriction to set
     */
    public void setWarnDeteriorationWithRestriction(boolean warnDeteriorationWithRestriction) {
        this.warnDeteriorationWithRestriction = warnDeteriorationWithRestriction;
    }

    /**
     * @return the warnDeteriorationTimeUntilRestriction
     */
    public double getWarnDeteriorationTimeUntilRestriction() {
        return warnDeteriorationTimeUntilRestriction;
    }

    /**
     * @param warnDeteriorationTimeUntilRestriction the warnDeteriorationTimeUntilRestriction to set
     */
    public void setWarnDeteriorationTimeUntilRestriction(double warnDeteriorationTimeUntilRestriction) {
        this.warnDeteriorationTimeUntilRestriction = warnDeteriorationTimeUntilRestriction;
    }

    /**
     * @return the warnForBreakTimeUntilWarning
     */
    public double getWarnForBreakTimeUntilWarning() {
        return warnForBreakTimeUntilWarning;
    }

    /**
     * @param warnForBreakTimeUntilWarning the warnForBreakTimeUntilWarning to set
     */
    public void setWarnForBreakTimeUntilWarning(double warnForBreakTimeUntilWarning) {
        this.warnForBreakTimeUntilWarning = warnForBreakTimeUntilWarning;
    }

    /**
     * @return the warnForBreakWithPopup
     */
    public boolean isWarnForBreakWithPopup() {
        return warnForBreakWithPopup;
    }

    /**
     * @param warnForBreakWithPopup the warnForBreakWithPopup to set
     */
    public void setWarnForBreakWithPopup(boolean warnForBreakWithPopup) {
        this.warnForBreakWithPopup = warnForBreakWithPopup;
    }

    /**
     * @return the warnForBreakWithSound
     */
    public boolean isWarnForBreakWithSound() {
        return warnForBreakWithSound;
    }

    /**
     * @param warnForBreakWithSound the warnForBreakWithSound to set
     */
    public void setWarnForBreakWithSound(boolean warnForBreakWithSound) {
        this.warnForBreakWithSound = warnForBreakWithSound;
    }

    /**
     * @return the warnForBreakWithRestriction
     */
    public boolean isWarnForBreakWithRestriction() {
        return warnForBreakWithRestriction;
    }

    /**
     * @param warnForBreakWithRestriction the warnForBreakWithRestriction to set
     */
    public void setWarnForBreakWithRestriction(boolean warnForBreakWithRestriction) {
        this.warnForBreakWithRestriction = warnForBreakWithRestriction;
    }

    /**
     * @return the warnForBreakTimeUntilRestriction
     */
    public double getWarnForBreakTimeUntilRestriction() {
        return warnForBreakTimeUntilRestriction;
    }

    /**
     * @param warnForBreakTimeUntilRestriction the warnForBreakTimeUntilRestriction to set
     */
    public void setWarnForBreakTimeUntilRestriction(double warnForBreakTimeUntilRestriction) {
        this.warnForBreakTimeUntilRestriction = warnForBreakTimeUntilRestriction;
    }

    /**
     * @return the warnForWorkTimeUntilWarning
     */
    public double getWarnForWorkTimeUntilWarning() {
        return warnForWorkTimeUntilWarning;
    }

    /**
     * @param warnForWorkTimeUntilWarning the warnForWorkTimeUntilWarning to set
     */
    public void setWarnForWorkTimeUntilWarning(double warnForWorkTimeUntilWarning) {
        this.warnForWorkTimeUntilWarning = warnForWorkTimeUntilWarning;
    }

    /**
     * @return the warnForWorkWithPopup
     */
    public boolean isWarnForWorkWithPopup() {
        return warnForWorkWithPopup;
    }

    /**
     * @param warnForWorkWithPopup the warnForWorkWithPopup to set
     */
    public void setWarnForWorkWithPopup(boolean warnForWorkWithPopup) {
        this.warnForWorkWithPopup = warnForWorkWithPopup;
    }

    /**
     * @return the warnForWorkWithSound
     */
    public boolean isWarnForWorkWithSound() {
        return warnForWorkWithSound;
    }

    /**
     * @param warnForWorkWithSound the warnForWorkWithSound to set
     */
    public void setWarnForWorkWithSound(boolean warnForWorkWithSound) {
        this.warnForWorkWithSound = warnForWorkWithSound;
    }

    
}
